package com.lv.phuotbacnam.ui.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.TextView;

import com.lv.phuotbacnam.R;
import com.lv.phuotbacnam.toolbox.TypeFaceUtils;

/**
 * TextView component supporting setting of different fonts from the assets
 */
public class FontTextView extends TextView{

    /**The directory in the assets where the fonts files should be put*/
    public static final String FONT_DIRECTORY = "fonts";

    /**The name of the font asset file*/
    private String typefaceAsset;

    public FontTextView(Context context) {
        super(context);
    }

    public FontTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        applyAttributes(context, attrs, 0);
    }


    public FontTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        applyAttributes(context, attrs, defStyle);
    }

    private void applyAttributes(Context context, AttributeSet attrs, int defStyle){
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FontTextView, defStyle, 0);
        setTypefaceAsset(a.getString(R.styleable.FontTextView_typefaceAsset));
        a.recycle();
    }

    public void setTypefaceAsset(String typefaceAsset) {
        this.typefaceAsset = typefaceAsset;
        setStyles();
    }

    private void setStyles(){
        if(typefaceAsset != null){
            setTypeface(TypeFaceUtils.get(getContext(), FONT_DIRECTORY + "/" + typefaceAsset));
        }
    }
}